<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="trueName">
        <el-input
          v-model="queryParams.trueName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采购单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入采购单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderState">
        <el-select v-model="queryParams.orderState" placeholder="请选择订单状态" clearable>
          <el-option value="0" label="已取消"></el-option>
          <el-option value="1" label="正常订单"></el-option>
          <el-option value="2" label="已经完成"></el-option>
          <el-option value="3" label="取消中"></el-option>
          <el-option value="4" label="超时"></el-option>
        </el-select>

<!--        <el-input-->
<!--          v-model="queryParams.orderState"-->
<!--          placeholder="订单状态"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery">-->
<!--        </el-input>-->
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable>
          <el-option value="1" label="货到付款"></el-option>
          <el-option value="2" label="在线支付"></el-option>
        </el-select>
<!--        <el-input-->
<!--          v-model="queryParams.payType"-->
<!--          placeholder="请输入支付方式"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
      </el-form-item>
      <el-form-item label="仓库编号" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="支付时间" prop="payTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.payTime"-->
<!--          type="daterange"-->
<!--          range-separator="至"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期">-->
<!--        </el-date-picker>-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.payTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择支付时间">-->
<!--        </el-date-picker>-->
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:inorderlist:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['member:inorderlist:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inorderlistList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="下单时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderCode"/>
      <el-table-column label="仓库编号" align="center" prop="warehouseCode"/>
      <el-table-column label="客户信息" align="center" prop="trueName"/>
      <el-table-column label="送货地址" align="center" prop="address"/>
      <el-table-column label="采购数量" align="center" prop="buyNum"/>
      <el-table-column label="金额" align="center" prop="amount"/>
      <el-table-column label="订单状态" align="center" prop="orderState">
        <template slot-scope="scope">
          {{ scope.row.orderState===0?'已取消':scope.row.orderState===1?'正常订单':scope.row.orderState===2?'已经完成':scope.row.orderState===3?'取消中':scope.row.orderState===4?'超时':scope.row.orderState===-99?'已删除':'状态有误' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="success"
            plain
            size="mini"
            @click="handlesee(scope.row.orderCode)"
          >打印
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['member:inorderlist:remove']"
          >删除
          </el-button>
          <el-button
            type="text"
            plain
            size="mini"
            @click="getOrderInfo(scope.row.orderCode)"
          >订单详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 展示出货单-->
    <el-dialog :visible.sync="open" append-to-body>
      <div id="dayin">
        <table>
          <tr>
            <th colspan="16">郑州市金水区瑞恒日用百货店销售出货单</th>
          </tr>
          <tr>
            <td colspan="9">开单日期: {{ inorderlistOne.payTime }}</td>
            <td colspan="7">单号: {{ inorderlistOne.orderCode }}</td>
          </tr>
          <tr>
            <td colspan="9">送货地址: {{ inorderlistOne.address }}</td>
            <td colspan="7">出货仓: {{ inorderlistOne.warehouseAddress }}</td>
          </tr>
        </table>
        <table class="bian">
          <tr class="bian">
            <td colspan="2" rowspan="2" class="bian">购货单</td>
            <td colspan="4" class="bian">名称</td>
            <td colspan="10" class="bian">{{ inorderlistOne.businessName }}</td>
          </tr>
          <tr>
            <td colspan="4" class="bian">电话</td>
            <td colspan="10" class="bian">{{ inorderlistOne.tel }}</td>
          </tr>
        </table>
        <table>
          <thead class="bian">
          <tr class="bian">
            <th colspan="6" class="bian">货品名称</th>
            <th colspan="3" class="bian">规格</th>
            <th colspan="1" class="bian">单位</th>
            <th colspan="1" class="bian">数量</th>
            <th colspan="1" class="bian">单价</th>
            <th colspan="2" class="bian">货款</th>
            <th colspan="2" class="bian">备注</th>
          </tr>
          </thead>
          <tbody v-for="item in inordergoodslistList">
          <tr class="bian">
            <td colspan="6" class="bian">{{ item.goodsName }}</td>
            <td colspan="3" class="bian">{{ item.specificationName }}</td>
            <td colspan="1" class="bian">{{ item.unit }}</td>
            <td colspan="1" class="bian">{{ item.buyNum }}</td>
            <td colspan="1" class="bian">{{ item.price }}</td>
            <td colspan="2" class="bian">{{ item.price * item.buyNum }}</td>
            <td colspan="2" class="bian">{{ item.remark }}</td>
          </tr>
          </tbody>
          <tr class="bian">
            <th colspan="4" class="bian">商品合计：</th>
            <td colspan="8" class="bian">总件数:{{ inorderlistOne.buyNum }}</td>
            <td colspan="2" class="bian">{{ inorderlistOne.amount }}</td>
            <td colspan="2" class="bian">备注</td>
          </tr>
          <tr class="bian">
            <th colspan="4" class="bian">物流费：</th>
            <td colspan="8" class="bian"></td>
            <td colspan="2" class="bian">{{ inorderlistOne.deliveryFee }}</td>
            <td colspan="2" class="bian">备注</td>
          </tr>
          <!--        <tr class="bian">-->
          <!--          <td colspan="4" class="bian">提货券：</td>-->
          <!--          <td colspan="8" class="bian"></td>-->
          <!--          <td colspan="2" class="bian">货款</td>-->
          <!--          <td colspan="2" class="bian">备注</td>-->
          <!--        </tr>-->
          <!--        <tr class="bian">-->
          <!--          <td colspan="4" class="bian">优惠金额：</td>-->
          <!--          <td colspan="8" class="bian"></td>-->
          <!--          <td colspan="2" class="bian">货款</td>-->
          <!--          <td colspan="2" class="bian">备注</td>-->
          <!--        </tr>-->
          <tr class="bian">
            <th colspan="4" class="bian">应付款：</th>
            <td colspan="8" class="bian"></td>
            <td colspan="2" class="bian">{{ inorderlistOne.amount }}</td>
            <td colspan="2" class="bian">备注</td>
          </tr>
          <tr class="bian">
            <th colspan="4" class="bian">支付方式：</th>
            <td colspan="8" class="bian">
              {{ inorderlistOne.payType === 1 ? '货到付款' : inorderlistOne.payType === 2 ? '在线支付' : '' }}
            </td>
            <td colspan="2" class="bian">{{ inorderlistOne.amount }}</td>
            <td colspan="2" class="bian">备注</td>
          </tr>
          <tr class="bian">
            <td colspan="4" class="bian">实付款：</td>
            <td colspan="8" class="bian" id="1">{{ inorderlistOne.bigAmount }}</td>
            <td colspan="2" class="bian" id="2">{{ inorderlistOne.allMoney }}</td>
            <td colspan="2" class="bian">备注</td>
          </tr>
        </table>
        <table>
          <tr>
            <td>客户签名：</td>
            <td>________</td>
            <td>仓库管理员：</td>
            <td> {{ inorderlistOne.nickName }}</td>
            <td>物流人员：</td>
            <td>________</td>
            <td>区域：</td>
            <td>________</td>
          </tr>
        </table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button class="filter-item"
                   size="mini"
                   type="primary"
                   icon="el-icon-printer"
                   @click="printme"
                   v-print>打印
        </el-button>
        <el-button @click="cancel">关闭</el-button>
      </div>
    </el-dialog>
  </div>

</template>
<style>
.bian {
  border: 1px solid black;
}

table {
  width: 600px;
  border-collapse: collapse;
}
</style>
<script>
import {delInorderlist, getInordergoodslist, getInorderlist, listInorderlist} from "@/api/member/inorderlist";
import Print from 'vue-print-nb';
import Vue from "vue";

Vue.use(Print);

export default {

  name: "Inorderlist",
  data() {
    return {
      m: '',
      bigAmount: '',
      num: 0,
      unit: new Array("仟", "佰", "拾", "", "仟", "佰", "拾", "", "角", "分"),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // #销售单明细数据
      inordergoodslistList: [],
      inorderlistOne: {},
      // #销售单管理表格数据
      inorderlistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderCode: null,
        sellerId: null,
        deliveryFee: null,
        amount: null,
        payType: null,
        orderStep: null,
        deliveryAddressId: null,
        payTime: null,
        orderState: null,
        orderStepRecord: null,
        warehouserId: null,
        logisticsId: null,
        mark: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderCode: [
          {required: true, message: "订单编号不能为空", trigger: "blur"}
        ],
        sellerId: [
          {required: true, message: "店铺id不能为空", trigger: "blur"}
        ],
        deliveryFee: [
          {required: true, message: "配送费不能为空", trigger: "blur"}
        ],
        amount: [
          {required: true, message: "订单金额不能为空", trigger: "blur"}
        ],
        orderStep: [
          {required: true, message: "订单步骤不能为空", trigger: "blur"}
        ],
        orderState: [
          {required: true, message: "订单状态不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    printme() {
      document.body.innerHTML = document.getElementById('dayin').innerHTML;
      window.print();
      location.reload();
      this.$router.push("/member/inorderlist/index")
    },
    /** 查询#销售单管理列表 */
    getList() {
      this.loading = true;
      listInorderlist(this.queryParams).then(response => {
        this.inorderlistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        orderCode: null,
        sellerId: null,
        deliveryFee: null,
        amount: null,
        payType: 0,
        orderStep: 0,
        deliveryAddressId: null,
        createTime: null,
        payTime: null,
        orderState: 0,
        orderStepRecord: null,
        warehouserId: null,
        logisticsId: null,
        mark: null,
        tip: true   //新增或修改标识 true添加 false修改
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderCode)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查看明细按钮操作 */
    handlesee(orderCode) {
      this.open = true;
      this.loading = true;
      getInorderlist(orderCode).then(response => {
        this.inorderlistOne = response.data;
        console.log(response.data)
        this.m = this.inorderlistOne.amount
        console.log(this.m)
        this.NumberToChinese(this.m)
      })
      getInordergoodslist(orderCode).then(response => {
        this.inordergoodslistList = response.rows;
        this.loading = false;
      });
    },

    /** 订单详情按钮操作 */
    getOrderInfo(orderCode) {
      this.$router.push("/member/inorderlist/orderprocess/"+orderCode)
    },


    /** 删除按钮操作 */
    handleDelete(row) {
      const orderCodes = row.orderCode || this.ids;
      this.$modal.confirm('是否确认删除#销售单管理编号为"' + orderCodes + '"的数据项？').then(function () {
        return delInorderlist(orderCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('member/inorderlist/export', {
        ...this.queryParams
      }, `inorderlist_${new Date().getTime()}.xlsx`)
    },

    toDx(n) {   //阿拉伯数字转换函数
      console.log(n)
      switch (n) {
        case "0":
          return "零";
        case "1":
          return "壹";
        case "2":
          return "贰";
        case "3":
          return "叁";
        case "4":
          return "肆";
        case "5":
          return "伍";
        case "6":
          return "陆";
        case "7":
          return "柒";
        case "8":
          return "捌";
        case "9":
          return "玖";
      }
    },

    NumberToChinese(m) {
      m *= 100;
      m += "";
      var length = m.length;

      var result = "";
      for (var i = 0; i < length; i++) {
        if (i == 2) {
          result = "元" + result;
        } else if (i == 6) {
          result = "万" + result;
        }
        if (m.charAt(length - i - 1) == 0) {
          if (i != 0 && i != 1) {
            if (result.charAt(0) != '零' && result.charAt(0) != '元' && result.charAt(0) != '万') {
              result = "零" + result;
            }
          }
          continue;
        }
        result = this.toDx(m.charAt(length - i - 1)) + this.unit[this.unit.length - i - 1] + result;
      }
      result += result.charAt(result.length - 1) == '元' ? "整" : "";
      console.log(result);
      this.inorderlistOne.bigAmount = result;
    }
  },

};

</script>
